package com.rongji.dfish.demo.editor.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rongji.dfish.base.Utils;
import com.rongji.dfish.framework.controller.BaseController;
import com.rongji.dfish.ui.widget.Leaf;
/**
 * 用要树的特色功能展示
 * 这个范例只是为了展示功能特色，所以没有分MVC 来写，并不利于工程的建立。
 * @author DFish Team
 *
 */
@Controller
@RequestMapping("/treeShow")
public class TreeShowController  extends BaseController{
	/**
	 * 展开数据
	 * @param request
	 * @return Object
	 * @throws Exception
	 */
	@RequestMapping("/open")
	@ResponseBody
	public Object open(HttpServletRequest request) throws Exception {
		boolean noDelay="1".equals(request.getParameter("nd"));
		String id=request.getParameter("id");
		Leaf shell=new Leaf();
		if(Utils.isEmpty(id)){
			id="000000";
		}
		List<String[]> subs=findSubData(id); 
		if(subs!=null){
			for(String[] sub:subs){
				Leaf leaf=new Leaf();
				shell.add(leaf);
				leaf.setId(sub[0]);
				leaf.setText(sub[1]+"("+sub[0]+")");
				//判定每个子节点是否可以展开。
				//如果是数据库的实现，这时候可能需要用exists每个父节点的ID。来提高性能。
				//这里只是简单粗暴的演示
				List<String[]> subs2=findSubData(sub[0]); 
				if(subs2!=null&&subs2.size()>0){
					leaf.setSrc("treeShow/open?id="+sub[0]+(noDelay?"&nd=1":""));
				}
			}
		}
		if(!noDelay){
			Thread.sleep(1000L);
		}
		return shell;
	}
	/**
	 * 展开数据
	 * @param request
	 * @return Object
	 * @throws Exception
	 */
	@RequestMapping("/openTo")
	@ResponseBody
	public Object openTo(HttpServletRequest request) throws Exception {
		boolean noDelay=true;
		String target=request.getParameter("id");
		Leaf shell=new Leaf();
		List<String> needToOpen=null;
		 if(target.endsWith("0000")){
			 needToOpen=Arrays.asList( "000000" );
		}else if(target.endsWith("00")){
			needToOpen=Arrays.asList( "000000",target.substring(0,2)+"0000" );
		}else {
			needToOpen=Arrays.asList( "000000",target.substring(0,2)+"0000" ,target.substring(0,4)+"00");
		}
		 Leaf nextShell=shell;
		for(int i=0;i<needToOpen.size();i++){
			String id=needToOpen.get(i);
			String nextId=target;
			if(i+1<needToOpen.size()){
				nextId=needToOpen.get(i+1);
			}
			List<String[]> subs=findSubData(id);
			Leaf temp=null;
			if(subs!=null){
				for(String[] sub:subs){
					Leaf leaf=new Leaf();
					nextShell.add(leaf);
					leaf.setId(sub[0]);
					leaf.setFocus(sub[0].equals(target));
					if(sub[0].equals(nextId)){
						temp=leaf;
					}
					leaf.setText(sub[1]+"("+sub[0]+")");
					//判定每个子节点是否可以展开。
					//如果是数据库的实现，这时候可能需要用exists每个父节点的ID。来提高性能。
					//这里只是简单粗暴的演示
					List<String[]> subs2=findSubData(sub[0]); 
					if(subs2!=null&&subs2.size()>0){
						leaf.setSrc("treeShow/open?id="+sub[0]+(noDelay?"&nd=1":""));
					}
				}
				nextShell=temp;
			}
		}
		
		return shell;
	}
	
	private List<String[]> findSubData(String id) {
		int level=0;
		if(id.endsWith("000000")){
			level=0;
		}else if(id.endsWith("0000")){
			level=2;
		}else if(id.endsWith("00")){
			level=4;
		}else {
			return null;
		}
		String prefix=id.substring(0,level);
		String postfix=id.substring(level+2,6);
		List<String[]>result=new ArrayList<String[]>();
		for(String[] row:DATA){
			if(row[0].startsWith(prefix)&&row[0].endsWith(postfix)&&!row[0].equals(id)){
				result.add(row);
			}
		}
		return result;
	}

	private static final String[][] DATA={
			{"350000","福建省"},
			{"350100","福州市"},
			{"350101","市辖区"},
			{"350102","鼓楼区"},
			{"350103","台江区"},
			{"350104","仓山区"},
			{"350105","马尾区"},
			{"350111","晋安区"},
			{"350121","闽侯县"},
			{"350122","连江县"},
			{"350123","罗源县"},
			{"350124","闽清县"},
			{"350125","永泰县"},
			{"350128","平潭县"},
			{"350181","福清市"},
			{"350182","长乐市"},
			{"350200","厦门市"},
			{"350201","市辖区"},
			{"350203","思明区"},
			{"350205","海沧区"},
			{"350206","湖里区"},
			{"350211","集美区"},
			{"350212","同安区"},
			{"350213","翔安区"},
			{"350500","泉州市"},
			{"350501","市辖区"},
			{"350502","鲤城区"},
			{"350503","丰泽区"},
			{"350504","洛江区"},
			{"350505","泉港区"},
			{"350521","惠安县"},
			{"350524","安溪县"},
			{"350525","永春县"},
			{"350526","德化县"},
			{"350527","金门县"},
			{"350581","石狮市"},
			{"350582","晋江市"},
			{"350583","南安市"},
			{"360000","江西省"},
			{"360100","南昌市"},
			{"360101","市辖区"},
			{"360102","东湖区"},
			{"360103","西湖区"},
			{"360104","青云谱区"},
			{"360105","湾里区"},
			{"360111","青山湖区"},
			{"360112","新建区"},
			{"360121","南昌县"},
			{"360123","安义县"},
			{"360124","进贤县"},
			{"360200","景德镇市"},
			{"360201","市辖区"},
			{"360202","昌江区"},
			{"360203","珠山区"},
			{"360222","浮梁县"},
			{"360281","乐平市"},
			{"360400","九江市"},
			{"360401","市辖区"},
			{"360402","庐山区"},
			{"360403","浔阳区"},
			{"360421","九江县"},
			{"360423","武宁县"},
			{"360424","修水县"},
			{"360425","永修县"},
			{"360426","德安县"},
			{"360427","星子县"},
			{"360428","都昌县"},
			{"360429","湖口县"},
			{"360430","彭泽县"},
			{"360481","瑞昌市"},
			{"360482","共青城市"},
	};
}
